package api

import (
	"blogdemo/gin/errmsg"
	"blogdemo/gin/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// ShowComment1 按文章ID查询
func ShowComment1(c *gin.Context) {
	arid := c.Param("articleId")
	var comment []model.Comment
	var flag bool
	comment, flag = model.GetCommentArticleId(arid)
	if !flag {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": errmsg.COMMENT_NO,
			"data":   comment,
			"msg":    errmsg.GetErrMsg(errmsg.COMMENT_NO),
		})
		fmt.Println("该文章下暂无评论")
		c.Abort()
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"status": errmsg.SUCCESS,
		"data":   comment,
		"msg":    errmsg.GetErrMsg(errmsg.SUCCESS),
	})
	fmt.Println("成功获取文章评论")
}

// ShowComment2 按文章标题查询
func ShowComment2(c *gin.Context) {
	arti := c.Param("articleTitle")
	var comment []model.Comment
	var flag bool
	comment, flag = model.GetCommentTitle(arti)
	if !flag {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": errmsg.COMMENT_NO,
			"data":   comment,
			"msg":    errmsg.GetErrMsg(errmsg.COMMENT_NO),
		})
		fmt.Println("该文章下暂无评论")
		c.Abort()
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"status": errmsg.SUCCESS,
		"data":   comment,
		"msg":    errmsg.GetErrMsg(errmsg.SUCCESS),
	})
	fmt.Println("成功获取文章评论")
}

// ShowComment3 按用户ID查询
func ShowComment3(c *gin.Context) {
	uid := c.Param("userId")
	var comment []model.Comment
	var flag bool
	comment, flag = model.GetCommentUserId(uid)
	if !flag {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": errmsg.COMMENT_NO,
			"data":   comment,
			"msg":    errmsg.GetErrMsg(errmsg.COMMENT_NO),
		})
		fmt.Println("该用户暂无评论")
		c.Abort()
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"status": errmsg.SUCCESS,
		"data":   comment,
		"msg":    errmsg.GetErrMsg(errmsg.SUCCESS),
	})
	fmt.Println("成功获取该用户评论")
}

// ShowComment4 按用户名查询
func ShowComment4(c *gin.Context) {
	uname := c.Param("username")
	var comment []model.Comment
	var flag bool
	comment, flag = model.GetCommentUser(uname)
	if !flag {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": errmsg.COMMENT_NO,
			"data":   comment,
			"msg":    errmsg.GetErrMsg(errmsg.COMMENT_NO),
		})
		fmt.Println("该用户暂无评论")
		c.Abort()
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"status": errmsg.SUCCESS,
		"data":   comment,
		"msg":    errmsg.GetErrMsg(errmsg.SUCCESS),
	})
	fmt.Println("成功获取该用户评论")
}
